/*
 * File    : /src/views/mark/meditor/components/PictureList/drawer.js
 * Time    : 2021-09-02 09:35:03
 * Author  : Gray Snail
 * Version : 1.0
 * Contact : shtao2011@sina.com
 * License : Copyright (c) 2021 Gray Snail
 * Desc    : None
 */

function drawRemark(remarkBallList, imageIndex, fontSize) {
  this.mainLayer[imageIndex].activate()
  remarkBallList.forEach((ball) => {
    if (ball.mType && ball.mType !== '3d') {
      let remarkIndex = parseInt(ball.mType.replace('2d-', ''))
      if (imageIndex === remarkIndex) {
        let errorDes = '验'
        let objectCenter = new Paper.Point(parseFloat(ball.x), parseFloat(ball.y))
        let textBackground = new Paper.Path()
        textBackground.add(objectCenter)
        textBackground.closed = true
        textBackground.strokeColor = 'red'
        textBackground.strokeWidth = 18
        textBackground.strokeCap = 'round'
        textBackground.strokeScaling = false
        textBackground.opacity = 1
        let remarkText = new Paper.PointText({
          position: objectCenter,
          fillColor: 'white',
          justification: 'center',
          fontSize: fontSize,
          locked: true,
        })
        remarkText.strokeWidth = 1
        remarkText.content = errorDes
        remarkText.opacity = 1
        remarkText.textBackground = textBackground
        textBackground.tag = ball.remark
        textBackground.typeTag = '错误描述：'
        textBackground.textObject = remarkText
        textBackground.markRemarkId = ball.remarkId
      }
    }
  })
}
function drawPoint(fontSize, imageIndex, annotObject) {
  let x, y
  let pointMark
  let globalPoint = false
  if (annotObject.twoD.other_attributes.a1 === 'point_3' || annotObject.markType === 'onepoint') {
    globalPoint = true
    x = this.imageWidth / 2
    y = this.imageHeight / 20
    fontSize = fontSize * 1.5
  } else {
    x = parseFloat(annotObject.twoD.points[0].x)
    y = parseFloat(annotObject.twoD.points[0].y)
  }

  // let remarkText = this.drawRemark(x, y, annotObject.twoD.other_attributes.a2, fontSize);
  pointMark = new Paper.Point(x, y)
  let pointView = new Paper.Path()
  pointView.add(pointMark)
  pointView.customCenter = pointMark
  pointView.closed = true
  if (annotObject.name + '_脑补点' === annotObject.twoD.other_attributes.a2) {
    pointView.strokeColor = 'red'
  } else {
    pointView.strokeColor = 'yellow'
  }
  pointView.strokeWidth = this.pointStrokeWidth
  pointView.strokeCap = 'round'
  pointView.markType = 'keypoints'
  pointView.strokeScaling = false
  pointView.opacity = 1
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(x, y - 2),
    fillColor: pointView.strokeColor,
    justification: 'center',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  // if(globalPoint === true) {
  let globalText = ''
  try {
    // let gArray = annotObject.twoD.other_attributes.a2.split(",");
    // for(let g = 0; g < gArray.length; g++) {
    //   try {
    //     let jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, "g" + g));
    //     globalText += (jsonObject.values[gArray[g]] ? jsonObject.values[gArray[g]] : gArray[g]) + "-";
    //   }
    //   catch{
    //     globalText += gArray[g] + "-";
    //   }
    // }
    let attrs = [
      annotObject.name,
      annotObject.twoD.other_attributes.a1,
      annotObject.twoD.other_attributes.a2,
      annotObject.twoD.other_attributes.a3,
      annotObject.twoD.other_attributes.a4,
      annotObject.twoD.other_attributes.a5,
      annotObject.twoD.other_attributes.a6,
      annotObject.twoD.other_attributes.a7,
      annotObject.twoD.other_attributes.a8,
    ]
    for (let i = 1; i <= 9; i++) {
      if (attrs[i] && attrs[i] != null && attrs[i] != '' && attrs[i] != 'null') {
        try {
          let jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, 'a' + i + '_2d'))
          let _values = jsonObject.values[attrs[i]]
          if (_values === undefined) continue
          globalText += '-' + _values
        } catch (e) {}
      }
    }
  } catch (err) {
    globalText = annotObject.twoD.other_attributes.a2 + '-'
  }
  descriptionText.content = annotObject.getLabelDescriptions(this.selectDictLabel, this.attributeNames)
  if (globalPoint === true) {
    pointView.opacity = 0
  }
  // else {
  //   descriptionText.content = annotObject.name;
  // }
  descriptionText.opacity = this.attributesTextOpacity
  descriptionText.pointView = pointView
  pointView.textObject = descriptionText
  pointView.tag =
    annotObject.name +
    ':2;' +
    annotObject.twoD.other_attributes.a1 +
    ';' +
    annotObject.twoD.other_attributes.a2 +
    ';' +
    annotObject.twoD.other_attributes.a3 +
    ';' +
    annotObject.twoD.other_attributes.a4 +
    ';' +
    annotObject.twoD.other_attributes.a5 +
    ';' +
    annotObject.twoD.other_attributes.a6 +
    ';' +
    annotObject.twoD.other_attributes.a7 +
    ';' +
    annotObject.twoD.other_attributes.a8 +
    ';'
  // pointView.missObject = remarkText;
  pointView.customCenter = {
    x: x,
    y: y,
  }
  pointView.mMarkAnnotationId = annotObject.mMarkAnnotationId
  return pointView
}
function drawRange(fontSize, imageIndex, annotObject) {
  var x1 = parseFloat(annotObject.twoD.bndbox.xmin)
  var y1 = parseFloat(annotObject.twoD.bndbox.ymin)
  var x2 = parseFloat(annotObject.twoD.bndbox.xmax)
  var y2 = parseFloat(annotObject.twoD.bndbox.ymax)
  // let remarkText = this.drawRemark(x1, y1, annotObject.twoD.other_attributes.a2, fontSize);
  let elementColor = '#00ff00' //this.selectDictLabel(this.colorNames, annotObject.name);
  let message = ''
  let attrs = [
    annotObject.name,
    annotObject.twoD.other_attributes.a1,
    annotObject.twoD.other_attributes.a2,
    annotObject.twoD.other_attributes.a3,
    annotObject.twoD.other_attributes.a4,
    annotObject.twoD.other_attributes.a5,
    annotObject.twoD.other_attributes.a6,
    annotObject.twoD.other_attributes.a7,
    annotObject.twoD.other_attributes.a8,
  ]
  let jsonObject
  let _values = undefined
  try {
    jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, 'labelName'))
    _values = jsonObject.values[annotObject.name]
  } catch (e) {}
  message += _values === undefined ? annotObject.name : _values
  if (annotObject.twoD.other_attributes.a1) message += '-' + annotObject.twoD.other_attributes.a1
  for (let i = 1; i <= 9; i++) {
    if (attrs[i] && attrs[i] != null && attrs[i] != '' && attrs[i] != 'null') {
      try {
        let jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, 'a' + i + '_2d'))
        let _values = jsonObject.values[attrs[i]]
        if (_values === undefined) continue
        message += '-' + _values
      } catch (e) {}
    }
  }
  let fac = parseFloat(Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) / 14)

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(Math.min(x1, x2) + fac, Math.min(y1, y2)),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  descriptionText.content = annotObject.twoD.other_attributes.a2
  descriptionText.opacity = this.attributesTextOpacity

  let path = new Paper.Path()
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.strokeColor = elementColor
  path.strokeWidth = 1
  path.strokeScaling = false
  path.closed = true
  path.opacity = 1
  path.tag = message
  path.textObject = descriptionText
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }
  // path.missObject = remarkText;
  path.mMarkAnnotationId = annotObject.mMarkAnnotationId

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a2, path)
  return path
}
function drawPoly(fontSize, imageIndex, annotObject, isLine) {
  let x1 = parseFloat(annotObject.twoD.points[0].x)
  let y1 = parseFloat(annotObject.twoD.points[0].y)
  let x2 = 0
  let y2 = 0
  let elementColor = this.selectDictLabel(this.colorNames, annotObject.name)
  let message = annotObject.getLabelDescriptions(this.selectDictLabel, this.attributeNames)

  let path = new Paper.Path()
  path.strokeColor = elementColor
  path.strokeWidth = this.lineStrokeWidth
  path.strokeScaling = false
  path.closed = !isLine
  path.opacity = 1
  if (isLine && (annotObject.name === 'Dash' || annotObject.name === 'Dash-SP')) {
    path.dashArray = [12, 6]
  } else {
    if (!isLine) {
      path.strokeWidth = this.polyStrokeWidth
      // // let basePath = path
      let fillPath = new Paper.Path()
      for (let p = 0; p < annotObject.twoD.points.length; p++) {
        fillPath.add(
          new Paper.Point(parseFloat(annotObject.twoD.points[p].x), parseFloat(annotObject.twoD.points[p].y))
        )
      }
      fillPath.strokeColor = elementColor
      fillPath.strokeWidth = 0.01
      fillPath.strokeScaling = false
      fillPath.closed = true
      fillPath.opacity = 0.3
      fillPath.fill = true
      fillPath.fillColor = elementColor
      path.fillPath = fillPath
      fillPath.markType = 'fillPath'
      fillPath.basePath = path
    }
  }
  path.markType = 'poly'
  path.points = new Array()
  for (let p = 0; p < annotObject.twoD.points.length; p++) {
    path.add(new Paper.Point(parseFloat(annotObject.twoD.points[p].x), parseFloat(annotObject.twoD.points[p].y)))
    x1 = Math.min(x1, parseFloat(annotObject.twoD.points[p].x))
    y1 = Math.min(y1, parseFloat(annotObject.twoD.points[p].y))
    x2 = Math.max(x2, parseFloat(annotObject.twoD.points[p].x))
    y2 = Math.max(y2, parseFloat(annotObject.twoD.points[p].y))

    // if (isLine) {
    let markPoint = new Paper.Path()
    markPoint.add(new Paper.Point(parseFloat(annotObject.twoD.points[p].x), parseFloat(annotObject.twoD.points[p].y)))
    markPoint.strokeColor = path.strokeColor
    markPoint.strokeWidth = path.strokeWidth
    // if (isLine) markPoint.strokeWidth = 2
    markPoint.strokeCap = 'round'
    markPoint.strokeScaling = false
    markPoint.closed = true
    markPoint.opacity = 1
    markPoint.markType = 'point'
    markPoint.pointIndex = 1000 + p
    markPoint.parentLabel = path
    path.points.push(markPoint)
    // }
  }
  path.tag = annotObject.getLabels(this.selectDictLabel, this.attributeNames)
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point((x1 + x2) / 2, (y1 + y2) / 2),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 2
  descriptionText.content = message
  descriptionText.opacity = this.attributesTextOpacity

  path.textObject = descriptionText

  path.mMarkAnnotationId = annotObject.mMarkAnnotationId

  path.pointToLocal = (point) => {
    return this.rasterLayer[imageIndex].globalToLocal(point)
  }

  path.hide = () => {
    path.textObject.opacity = 0
    path.opacity = 0
    if (path.fillPath) {
      path.fillPath.opacity = 0
    }
    if (path.points) {
      path.points.forEach((p) => {
        p.opacity = 0
      })
    }
  }
  path.show = () => {
    path.textObject.opacity = 1
    path.opacity = 1
    if (path.fillPath) {
      path.opacity = 0.01
      path.fillPath.opacity = 1
    }
    if (path.points) {
      path.points.forEach((p) => {
        p.opacity = 1
      })
    }
  }

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)
  return path
}
function drawRect(fontSize, imageIndex, annotObject) {
  /*
    0----6
    |    |\
    |    | \
    |    |  \
    |    |   7
    |    |   |
    |    |   |
    |    |   5
    |    | 3/
    |    |2/
    |    |/
    -----4    1
    */

  let x1 = Math.min(parseFloat(annotObject.twoD.bndbox.xmin), parseFloat(annotObject.twoD.bndbox.xmax))
  let y1 = Math.min(parseFloat(annotObject.twoD.bndbox.ymin), parseFloat(annotObject.twoD.bndbox.ymax))
  let x2 = Math.max(parseFloat(annotObject.twoD.bndbox.xmin), parseFloat(annotObject.twoD.bndbox.xmax))
  let y2 = Math.max(parseFloat(annotObject.twoD.bndbox.ymin), parseFloat(annotObject.twoD.bndbox.ymax))
  let elementColor = this.selectDictLabel(this.colorNames, annotObject.name)
  let message = annotObject.getLabelDescriptions(this.selectDictLabel, this.attributeNames)
  if (this.selectDictLabel(this.attributeNames, 'size_2d') == 'true') {
    message += '宽x高：' + Math.abs(x2 - x1) + 'x' + Math.abs(y2 - y1)
  }
  let fac = parseFloat(Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) / 14)

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(Math.min(x1, x2) + fac, Math.min(y1, y2)),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  descriptionText.content = message
  descriptionText.opacity = this.attributesTextOpacity

  // let remarkText = this.drawRemark((x1 + x2) / 2, (y1 + y2) / 2, annotObject.twoD.other_attributes.a1 + '-2d-' + imageIndex, fontSize);

  let path = new Paper.Path()
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.strokeColor = elementColor
  path.strokeWidth = this.rectStrokeWidth
  path.strokeScaling = false
  path.closed = true
  path.opacity = 1
  let baseRect = path

  path = new Paper.Path()
  path.data.baseRect = baseRect
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.closed = true
  path.fillColor = elementColor
  path.selectedColor = elementColor
  if (!path.fillColor) {
    path.fillColor = 'rgb(0,255,255)'
  }
  if (annotObject.twoD.bndbox.xsmin) {
    path.opacity = 0.1
    // path.fillColor = 'white';
    path.fillColor = elementColor
  } else path.opacity = 0
  path.tag = annotObject.getLabels(this.selectDictLabel, this.attributeNames)
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }

  path.textObject = descriptionText
  // if(this.markType !== 10)
  //   path.missObject = remarkText;

  // let opacity = this.selectedObject && this.selectedObject.a1 == attrs[1] ? 1 : 0;
  let point1 = new Paper.Path()
  point1.add(new Paper.Point(x1, y1))
  point1.strokeColor = elementColor
  point1.strokeWidth = this.pointStrokeWidth
  point1.strokeScaling = false
  point1.closed = true
  // point1.opacity = opacity;
  point1.markType = 'point'
  point1.strokeCap = 'round'
  point1.tag = path.tag
  point1.customCenter = {
    x: x1,
    y: y1,
  }
  point1.parentLabel = path
  point1.pointIndex = 0
  point1.parentLabel = path
  path.point1 = point1

  let point3 = new Paper.Path()
  point3.add(new Paper.Point(x2, y2))
  point3.strokeColor = elementColor
  point3.strokeWidth = this.pointStrokeWidth
  point3.strokeScaling = false
  point3.closed = true
  // point3.opacity = opacity;
  point3.markType = 'point'
  point3.strokeCap = 'round'
  point3.tag = path.tag
  point3.customCenter = {
    x: x2,
    y: y2,
  }
  point3.parentLabel = path
  point3.pointIndex = 1
  point3.parentLabel = path
  path.point3 = point3

  path.markType = 'rect'

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)

  if (annotObject.twoD.bndbox.xlmin) {
    let xlmin = Math.min(parseFloat(annotObject.twoD.bndbox.xlmin), parseFloat(annotObject.twoD.bndbox.xlmax))
    let ylmin = Math.min(parseFloat(annotObject.twoD.bndbox.ylmin), parseFloat(annotObject.twoD.bndbox.ylmax))
    let xlmax = Math.max(parseFloat(annotObject.twoD.bndbox.xlmin), parseFloat(annotObject.twoD.bndbox.xlmax))
    let ylmax = Math.max(parseFloat(annotObject.twoD.bndbox.ylmin), parseFloat(annotObject.twoD.bndbox.ylmax))
    let rangePath = new Paper.Path()
    rangePath.add(new Paper.Point(xlmin, ylmin))
    rangePath.add(new Paper.Point(xlmax, ylmax))
    rangePath.strokeColor = elementColor
    rangePath.strokeWidth = 2
    rangePath.strokeScaling = false
    rangePath.closed = false
    rangePath.opacity = 1
    rangePath.markType = 'rect_line'
    rangePath.tag = path.tag
    rangePath.parentLabel = path
    rangePath.customCenter = {
      x: (xlmin + xlmax) / 2,
      y: (ylmin + ylmax) / 2,
    }

    let rangePoint1 = new Paper.Path()
    rangePoint1.add(new Paper.Point(xlmin, ylmin))
    rangePoint1.strokeColor = 'rgb(255,0,0)'
    rangePoint1.strokeWidth = this.pointStrokeWidth
    rangePoint1.strokeScaling = false
    rangePoint1.closed = true
    rangePoint1.opacity = 1
    rangePoint1.markType = 'point'
    rangePoint1.strokeCap = 'round'
    rangePoint1.tag = path.tag
    rangePoint1.customCenter = {
      x: xlmin,
      y: ylmin,
    }
    rangePoint1.pointIndex = 3
    rangePoint1.parentLabel = rangePath
    rangePath.rangePoint1 = rangePoint1

    let rangePoint2 = new Paper.Path()
    rangePoint2.add(new Paper.Point(xlmax, ylmax))
    rangePoint2.strokeColor = 'rgb(255,0,0)'
    rangePoint2.strokeWidth = this.pointStrokeWidth
    rangePoint2.strokeScaling = false
    rangePoint2.closed = true
    rangePoint2.opacity = 1
    rangePoint2.markType = 'point'
    rangePoint2.strokeCap = 'round'
    rangePoint2.tag = path.tag
    rangePoint2.customCenter = {
      x: xlmax,
      y: ylmax,
    }
    rangePoint2.pointIndex = 4
    rangePoint2.parentLabel = rangePath
    rangePath.rangePoint2 = rangePoint2

    path.rangePath = rangePath

    rangePath = new Paper.Path()
    if (xlmin >= x2) rangePath.add(new Paper.Point(x2, y1))
    else rangePath.add(new Paper.Point(x1, y1))
    rangePath.add(new Paper.Point(xlmin, ylmin))
    rangePath.strokeColor = elementColor
    rangePath.strokeWidth = 2
    rangePath.strokeScaling = false
    rangePath.closed = false
    rangePath.opacity = 1
    rangePath.markType = 'rect_line'
    rangePath.tag = path.tag
    rangePath.parentLabel = path
    rangePath.customCenter = {
      x: (xlmin + xlmax) / 2,
      y: (ylmin + ylmax) / 2,
    }

    rangePath = new Paper.Path()
    if (xlmin >= x2) rangePath.add(new Paper.Point(x2, y2))
    else rangePath.add(new Paper.Point(x1, y2))
    rangePath.add(new Paper.Point(xlmax, ylmax))
    rangePath.strokeColor = elementColor
    rangePath.strokeWidth = 2
    rangePath.strokeScaling = false
    rangePath.closed = false
    rangePath.opacity = 1
    rangePath.markType = 'rect_line'
    rangePath.tag = path.tag
    rangePath.parentLabel = path
    rangePath.customCenter = {
      x: (xlmin + xlmax) / 2,
      y: (ylmin + ylmax) / 2,
    }
  }

  if (annotObject.twoD.bndbox.xsmin) {
    let x3 = Math.min(parseFloat(annotObject.twoD.bndbox.xsmin), parseFloat(annotObject.twoD.bndbox.xsmax))
    let y3 = Math.min(parseFloat(annotObject.twoD.bndbox.ysmin), parseFloat(annotObject.twoD.bndbox.ysmax))
    let x4 = Math.max(parseFloat(annotObject.twoD.bndbox.xsmin), parseFloat(annotObject.twoD.bndbox.xsmax))
    let y4 = Math.max(parseFloat(annotObject.twoD.bndbox.ysmin), parseFloat(annotObject.twoD.bndbox.ysmax))
    let pathArray = []
    let tmpPath = new Paper.Path()
    if (y1 < y3) tmpPath.dashArray = [3, 5]
    tmpPath.add(new Paper.Point(x3, y3))
    tmpPath.add(new Paper.Point(x4, y3))
    tmpPath.add(new Paper.Point(x2, y1))
    tmpPath.strokeColor = elementColor
    tmpPath.strokeWidth = 1
    tmpPath.strokeScaling = false
    tmpPath.closed = false
    tmpPath.opacity = 1
    pathArray.push(tmpPath)

    tmpPath = new Paper.Path()
    if (x1 < x3 && y1 < y3) tmpPath.dashArray = [3, 5]
    tmpPath.add(new Paper.Point(x1, y1))
    tmpPath.add(new Paper.Point(x3, y3))
    tmpPath.strokeColor = elementColor
    tmpPath.strokeWidth = 1
    tmpPath.strokeScaling = false
    tmpPath.closed = false
    tmpPath.opacity = 1
    pathArray.push(tmpPath)

    tmpPath = new Paper.Path()
    if (x1 < x3) tmpPath.dashArray = [3, 5]
    tmpPath.add(new Paper.Point(x3, y3))
    tmpPath.add(new Paper.Point(x3, y4))
    tmpPath.add(new Paper.Point(x1, y2))
    tmpPath.strokeColor = elementColor
    tmpPath.strokeWidth = 1
    tmpPath.strokeScaling = false
    tmpPath.closed = false
    tmpPath.opacity = 1
    pathArray.push(tmpPath)

    tmpPath = new Paper.Path()
    tmpPath.dashArray = [3, 5]
    tmpPath.add(new Paper.Point(x3, y4))
    tmpPath.add(new Paper.Point(x4, y4))
    tmpPath.strokeColor = elementColor
    tmpPath.strokeWidth = 1
    tmpPath.strokeScaling = false
    tmpPath.closed = false
    tmpPath.opacity = 1
    pathArray.push(tmpPath)

    tmpPath = new Paper.Path()
    if (x4 < x2) tmpPath.dashArray = [3, 5]
    tmpPath.add(new Paper.Point(x4, y3))
    tmpPath.add(new Paper.Point(x4, y4))
    tmpPath.add(new Paper.Point(x2, y2))
    tmpPath.strokeColor = elementColor
    tmpPath.strokeWidth = 1
    tmpPath.strokeScaling = false
    tmpPath.closed = false
    tmpPath.opacity = 1
    pathArray.push(tmpPath)

    path.pathArray = pathArray
    if (this.markType == 10) {
      let xBoundMin = Math.min(x1, x3)
      let xBoundMax = Math.max(x2, x4)
      let yBoundMin = Math.min(y1, y3)
      let yBoundMax = Math.max(y2, y4)
      tmpPath = new Paper.Path()
      tmpPath.dashArray = [8, 3]
      tmpPath.add(new Paper.Point(xBoundMin, yBoundMin))
      tmpPath.add(new Paper.Point(xBoundMax, yBoundMin))
      tmpPath.add(new Paper.Point(xBoundMax, yBoundMax))
      tmpPath.add(new Paper.Point(xBoundMin, yBoundMax))
      tmpPath.strokeColor = 'blue'
      tmpPath.strokeWidth = 2
      tmpPath.strokeScaling = false
      tmpPath.closed = true
      tmpPath.opacity = this.attributesTextOpacity
      pathArray.push(tmpPath)
      path.rangeHelper = tmpPath
    }
  }

  path.mMarkAnnotationId = annotObject.mMarkAnnotationId
  path.pointToLocal = (point) => {
    return this.rasterLayer[imageIndex].globalToLocal(point)
  }
  path.hide = () => {
    path.textObject.opacity = 0
    path.data.baseRect.opacity = 0
    if (path.rangePath) {
      path.rangePath.opacity = 0
    }
    if (path.pathArray) {
      path.pathArray.forEach((p) => {
        p.opacity = 0
      })
    }
    if (path.opacity == 0.1) {
      path.opacity = 0.001
    }
    if (path.rangeHelper) {
      path.rangeHelper.opacity = 0
    }
  }
  path.show = () => {
    path.textObject.opacity = 1
    path.data.baseRect.opacity = 1
    if (path.rangePath) {
      path.rangePath.opacity = 1
    }
    if (path.pathArray) {
      path.pathArray.forEach((p) => {
        p.opacity = 1
      })
    }
    if (path.opacity == 0.001) {
      path.opacity = 0.1
    }
    if (path.rangeHelper) {
      path.rangeHelper.opacity = 1
    }
  }
  return path
}
function drawRectPoint1(fontSize, imageIndex, annotObject) {
  let x1 = Math.min(parseFloat(annotObject.twoD.bndbox.xmin), parseFloat(annotObject.twoD.bndbox.xmax))
  let y1 = Math.min(parseFloat(annotObject.twoD.bndbox.ymin), parseFloat(annotObject.twoD.bndbox.ymax))
  let x2 = Math.max(parseFloat(annotObject.twoD.bndbox.xmin), parseFloat(annotObject.twoD.bndbox.xmax))
  let y2 = Math.max(parseFloat(annotObject.twoD.bndbox.ymin), parseFloat(annotObject.twoD.bndbox.ymax))
  let pointX = parseFloat(annotObject.twoD.bndbox.pointX)
  let pointY = parseFloat(annotObject.twoD.bndbox.pointY)
  let elementColor = this.selectDictLabel(this.colorNames, annotObject.name)
  let message = annotObject.getLabelDescriptions(this.selectDictLabel, this.attributeNames)
  if (this.selectDictLabel(this.attributeNames, 'size_2d') == 'true') {
    message += '宽x高：' + Math.abs(x2 - x1).toFixed(2) + 'x' + Math.abs(y2 - y1).toFixed(2)
  }
  let fac = parseFloat(Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) / 14)

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(Math.min(x1, x2) + fac, Math.min(y1, y2)),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  descriptionText.content = message
  descriptionText.opacity = this.attributesTextOpacity

  // let remarkText = this.drawRemark((x1 + x2) / 2, (y1 + y2) / 2, annotObject.twoD.other_attributes.a1 + '-2d-' + imageIndex, fontSize);

  let path = new Paper.Path()
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.strokeColor = elementColor
  path.strokeWidth = this.rectStrokeWidth
  path.strokeScaling = false
  path.closed = true
  path.opacity = 1
  let baseRect = path

  path = new Paper.Path()
  path.data.baseRect = baseRect
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.closed = true
  path.fillColor = elementColor
  path.selectedColor = elementColor
  if (!path.fillColor) {
    path.fillColor = 'rgb(0,255,255)'
  }
  if (annotObject.twoD.bndbox.xsmin) {
    path.opacity = 0.1
    path.fillColor = elementColor
  } else path.opacity = 0
  path.tag = annotObject.getLabels(this.selectDictLabel, this.attributeNames)
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }

  path.textObject = descriptionText
  // if(this.markType !== 10) {
  //   path.missObject = remarkText;
  // }
  let point1 = new Paper.Path()
  point1.add(new Paper.Point(x1, y1))
  point1.strokeColor = elementColor
  point1.strokeWidth = this.pointStrokeWidth
  point1.strokeScaling = false
  point1.closed = true
  point1.markType = 'point'
  point1.strokeCap = 'round'
  point1.tag = path.tag
  point1.customCenter = {
    x: x1,
    y: y1,
  }
  point1.parentLabel = path
  point1.pointIndex = 0
  point1.parentLabel = path
  path.point1 = point1

  let point3 = new Paper.Path()
  point3.add(new Paper.Point(x2, y2))
  point3.strokeColor = elementColor
  point3.strokeWidth = this.pointStrokeWidth
  point3.strokeScaling = false
  point3.closed = true
  point3.markType = 'point'
  point3.strokeCap = 'round'
  point3.tag = path.tag
  point3.customCenter = {
    x: x2,
    y: y2,
  }
  point3.parentLabel = path
  point3.pointIndex = 1
  path.point3 = point3

  let rectPoint1 = new Paper.Path()
  rectPoint1.add(new Paper.Point(pointX, pointY))
  rectPoint1.strokeColor = elementColor
  rectPoint1.strokeWidth = this.pointStrokeWidth
  rectPoint1.strokeScaling = false
  rectPoint1.closed = true
  rectPoint1.markType = 'rectPoint1'
  rectPoint1.strokeCap = 'round'
  rectPoint1.tag = path.tag
  rectPoint1.customCenter = {
    x: x2,
    y: y2,
  }
  rectPoint1.parentLabel = path
  path.rectPoint1 = rectPoint1

  path.markType = 'rect'

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)

  path.mMarkAnnotationId = annotObject.mMarkAnnotationId
  path.pointToLocal = (point) => {
    return this.rasterLayer[imageIndex].globalToLocal(point)
  }
  path.hide = () => {
    path.textObject.opacity = 0
    path.data.baseRect.opacity = 0
    path.point1.opacity = 0
    path.point3.opacity = 0
    path.rectPoint1.opacity = 0
    if (path.opacity == 0.1) {
      path.opacity = 0.001
    }
  }
  path.show = () => {
    path.textObject.opacity = 1
    path.data.baseRect.opacity = 1
    path.point1.opacity = 1
    path.point3.opacity = 1
    path.rectPoint1.opacity = 1
    if (path.opacity == 0.001) {
      path.opacity = 0.1
    }
  }
  return path
}
function drawGrid(fontSize, imageIndex, annotObject) {
  let x1 = parseFloat(annotObject.twoD.points[0].x)
  let y1 = parseFloat(annotObject.twoD.points[0].y)
  let x2 = parseFloat(annotObject.twoD.points[1].x)
  let y2 = parseFloat(annotObject.twoD.points[1].y)
  let elementColor = this.selectDictLabel(this.colorNames, annotObject.name)
  let message = annotObject.getLabelDescriptions(this.selectDictLabel, this.attributeNames)
  if (this.selectDictLabel(this.attributeNames, 'size_2d') == 'true') {
    message += '宽x高：' + Math.abs(x2 - x1) + 'x' + Math.abs(y2 - y1)
  }
  let fac = parseFloat(Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) / 14)

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(Math.min(x1, x2) + fac, Math.min(y1, y2)),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  descriptionText.content = message
  descriptionText.opacity = this.attributesTextOpacity

  // let remarkText = this.drawRemark((x1 + x2) / 2, (y1 + y2) / 2, annotObject.twoD.other_attributes.a1 + '-2d-' + imageIndex, fontSize);

  let path = new Paper.Path()
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.strokeColor = elementColor
  path.strokeWidth = 1
  path.strokeScaling = false
  path.closed = true
  path.opacity = 1
  let baseRect = path

  path = new Paper.Path()
  path.data.baseRect = baseRect
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.closed = true
  path.fillColor = elementColor
  path.selectedColor = elementColor
  if (!path.fillColor) {
    path.fillColor = 'rgb(0,255,255)'
  }
  path.tag = annotObject.getLabels(this.selectDictLabel, this.attributeNames)
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }
  path.opacity = 0.001

  path.textObject = descriptionText
  // if(this.markType !== 10)
  //   path.missObject = remarkText;

  let point1 = new Paper.Path()
  point1.add(new Paper.Point(x1, y1))
  point1.strokeColor = elementColor
  point1.strokeWidth = this.pointStrokeWidth
  point1.strokeScaling = false
  point1.closed = true
  point1.markType = 'point'
  point1.strokeCap = 'round'
  point1.tag = path.tag
  point1.customCenter = {
    x: x1,
    y: y1,
  }
  point1.parentLabel = path
  point1.pointIndex = 0
  point1.parentLabel = path
  path.point1 = point1

  let point3 = new Paper.Path()
  point3.add(new Paper.Point(x2, y2))
  point3.strokeColor = elementColor
  point3.strokeWidth = this.pointStrokeWidth
  point3.strokeScaling = false
  point3.closed = true
  point3.markType = 'point'
  point3.strokeCap = 'round'
  point3.tag = path.tag
  point3.customCenter = {
    x: x2,
    y: y2,
  }
  point3.parentLabel = path
  point3.pointIndex = 1
  point3.parentLabel = path
  path.point3 = point3

  path.markType = 'grid'

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)

  for (let p = 2; p < annotObject.twoD.points.length; p++) {
    let line = new Paper.Path()
    line.add(new Paper.Point(parseFloat(annotObject.twoD.points[p].x), parseFloat(annotObject.twoD.points[p].y)))
    p++
    line.add(new Paper.Point(parseFloat(annotObject.twoD.points[p].x), parseFloat(annotObject.twoD.points[p].y)))
    line.strokeColor = elementColor
    line.strokeWidth = 1
    line.strokeScaling = false
    line.closed = false
    line.opacity = 1
    line.markType = 'gridLine'
  }

  path.textObject = descriptionText

  path.pointToLocal = (point) => {
    return this.rasterLayer[imageIndex].globalToLocal(point)
  }

  path.hide = () => {
    path.textObject.opacity = 0
    path.opacity = 0
    if (path.points)
      path.points.forEach((p) => {
        p.opacity = 0
      })
  }
  path.show = () => {
    path.textObject.opacity = 1
    path.opacity = 1
    if (path.points)
      path.points.forEach((p) => {
        p.opacity = 1
      })
  }

  path.mMarkAnnotationId = annotObject.mMarkAnnotationId
  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)
  return path
}
function drawRect3d(fontSize, imageIndex, annotObject) {
  let x1 = parseFloat(annotObject.twoD.bndbox.xmin)
  let y1 = parseFloat(annotObject.twoD.bndbox.ymin)
  let x2 = parseFloat(annotObject.twoD.bndbox.xmax)
  let y2 = parseFloat(annotObject.twoD.bndbox.ymax)
  let elementColor = this.selectDictLabel(this.colorNames, annotObject.name)
  let message = ''
  let pointText = ''
  let pointTag = annotObject.name + ':2;'
  let attrs = [
    annotObject.name,
    annotObject.twoD.other_attributes.a1,
    annotObject.twoD.other_attributes.a2,
    annotObject.twoD.other_attributes.a3,
    annotObject.twoD.other_attributes.a4,
    annotObject.twoD.other_attributes.a5,
    annotObject.twoD.other_attributes.a6,
    annotObject.twoD.other_attributes.a7,
    annotObject.twoD.other_attributes.a8,
    annotObject.twoD.other_attributes.b1,
    annotObject.twoD.other_attributes.b2,
    annotObject.twoD.other_attributes.b3,
    annotObject.twoD.other_attributes.b4,
    annotObject.twoD.other_attributes.b5,
    annotObject.twoD.other_attributes.b6,
    annotObject.twoD.other_attributes.b7,
    annotObject.twoD.other_attributes.b8,
  ]
  let jsonObject
  let _values = undefined
  try {
    jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, 'labelName'))
    _values = jsonObject.values[annotObject.name]
  } catch (e) {}
  message += _values === undefined ? annotObject.name : _values
  if (annotObject.twoD.other_attributes.a1) message += '-' + annotObject.twoD.other_attributes.a1
  for (let i = 1; i <= 9; i++) {
    if (attrs[i] && attrs[i] != null && attrs[i] != '' && attrs[i] != 'null') {
      try {
        let jsonObject = JSON.parse(this.selectDictLabel(this.attributeNames, 'a' + i + '_2d'))
        let _values = jsonObject.values[attrs[i]]
        if (_values === undefined) continue
        if (!jsonObject.pointAttribute || jsonObject.pointAttribute == 'false') {
          message += '-' + _values
          pointTag += ';'
        } else {
          pointText += '-' + _values
          pointTag += attrs[i] + ';'
        }
      } catch (e) {
        if (!jsonObject.pointAttribute || jsonObject.pointAttribute == 'false') message += '-' + attrs[i]
        else pointText += '-' + attrs[i]
      }
    }
  }
  if (this.selectDictLabel(this.attributeNames, 'size_2d') == 'true') {
    message += '宽x高：' + parseInt(Math.abs(x2 - x1)) + 'x' + parseInt(Math.abs(y2 - y1))
  }
  let fac = parseFloat(Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) / 14)

  // 标注属性描述文字
  let descriptionText = new Paper.PointText({
    position: new Paper.Point(Math.min(x1, x2) + fac, Math.min(y1, y2)),
    fillColor: elementColor,
    justification: 'left',
    fontSize: fontSize,
    locked: true,
  })
  descriptionText.strokeWidth = 1
  descriptionText.content = message
  descriptionText.opacity = this.attributesTextOpacity

  // let remarkText = this.drawRemark((x1 + x2) / 2, (y1 + y2) / 2, annotObject.twoD.other_attributes.a1 + '-2d-' + imageIndex, fontSize);

  let path = new Paper.Path()
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.strokeColor = elementColor
  path.strokeWidth = this.rectStrokeWidth
  path.strokeScaling = false
  path.closed = true
  path.opacity = 1
  let baseRect = path

  path = new Paper.Path()
  path.data.baseRect = baseRect
  path.add(new Paper.Point(x1, y1))
  path.add(new Paper.Point(x2, y1))
  path.add(new Paper.Point(x2, y2))
  path.add(new Paper.Point(x1, y2))
  path.closed = true
  path.fillColor = elementColor
  path.selectedColor = elementColor
  if (!path.fillColor) {
    path.fillColor = 'rgb(0,255,255)'
  }
  path.opacity = 0
  path.tag =
    annotObject.name +
    ':2;' +
    annotObject.twoD.other_attributes.a1 +
    ';' +
    annotObject.twoD.other_attributes.a2 +
    ';' +
    annotObject.twoD.other_attributes.a3 +
    ';' +
    annotObject.twoD.other_attributes.a4 +
    ';' +
    annotObject.twoD.other_attributes.a5 +
    ';' +
    annotObject.twoD.other_attributes.a6 +
    ';' +
    annotObject.twoD.other_attributes.a7 +
    ';' +
    annotObject.twoD.other_attributes.a8 +
    ';'
  path.customCenter = {
    x: (x1 + x2) / 2,
    y: (y1 + y2) / 2,
  }

  path.textObject = descriptionText
  // if(this.markType !== 10)
  //   path.missObject = remarkText;

  // let opacity = this.selectedObject && this.selectedObject.a1 == attrs[1] ? 1 : 0;
  let point1 = new Paper.Path()
  point1.add(new Paper.Point(x1, y1))
  point1.strokeColor = path.fillColor
  point1.strokeWidth = this.pointStrokeWidth
  point1.strokeScaling = false
  point1.closed = true
  // point1.opacity = opacity;
  point1.markType = 'point'
  point1.strokeCap = 'round'
  point1.tag = path.tag
  point1.customCenter = {
    x: x1,
    y: y1,
  }
  point1.parentLabel = path
  point1.pointIndex = 0
  point1.parentLabel = path
  path.point1 = point1

  let point3 = new Paper.Path()
  point3.add(new Paper.Point(x2, y2))
  point3.strokeColor = path.fillColor
  point3.strokeWidth = this.pointStrokeWidth
  point3.strokeScaling = false
  point3.closed = true
  // point3.opacity = opacity;
  point3.markType = 'point'
  point3.strokeCap = 'round'
  point3.tag = path.tag
  point3.customCenter = {
    x: x2,
    y: y2,
  }
  point3.parentLabel = path
  point3.pointIndex = 1
  point3.parentLabel = path
  path.point3 = point3

  path.markType = 'rect'

  this.annotation2dMap[imageIndex].set(annotObject.twoD.other_attributes.a1, path)
  let gPoint = this.selectDictLabel(this.attributeNames, 'g_point')
  if (annotObject.twoD.bndbox.xmid) {
    let xmid = parseFloat(annotObject.twoD.bndbox.xmid)
    let ymid = parseFloat(annotObject.twoD.bndbox.ymid)
    let midPath = new Paper.Path()
    if (gPoint && gPoint === 'true') {
      midPath.add(new Paper.Point(xmid, ymid))
      midPath.add(new Paper.Point(xmid, ymid))
    } else {
      midPath.add(new Paper.Point(xmid, y1))
      midPath.add(new Paper.Point(xmid, y2))
    }
    // midPath.strokeColor = elementColor;
    midPath.strokeColor = 'rgb(0,255,0)'
    midPath.strokeWidth = 2
    midPath.strokeScaling = false
    midPath.closed = false
    midPath.opacity = 1
    midPath.markType = 'rect_mid'
    midPath.tag = path.tag
    midPath.parentLabel = path
    midPath.customCenter = {
      x: xmid,
      y: ymid,
    }

    let pointMid = new Paper.Path()
    if (gPoint && gPoint === 'true') {
      pointMid.add(new Paper.Point(xmid, ymid))
      pointMid.tag = pointTag
    } else {
      pointMid.add(new Paper.Point(xmid, (y1 + y2) / 2))
      pointMid.tag = path.tag
    }

    pointMid.strokeColor = 'rgb(0,255,0)'
    pointMid.strokeWidth = this.pointStrokeWidth
    pointMid.strokeScaling = false
    pointMid.closed = true
    pointMid.opacity = 1
    pointMid.markType = 'point'
    pointMid.strokeCap = 'round'
    pointMid.customCenter = {
      x: xmid,
      y: ymid,
    }
    pointMid.pointIndex = 2
    // 标注属性描述文字
    let pText = new Paper.PointText({
      position: new Paper.Point(xmid, ymid),
      fillColor: elementColor,
      justification: 'left',
      fontSize: fontSize,
      locked: true,
    })
    pText.strokeWidth = 1
    pText.content = pointText
    pText.opacity = this.attributesTextOpacity
    pointMid.textObject = pText
    pointMid.midPath = midPath
    midPath.pointMid = pointMid
    path.midPath = midPath
  }
  if (annotObject.twoD.bndbox.xlmin) {
    let xlmin = parseFloat(annotObject.twoD.bndbox.xlmin)
    let ylmin = parseFloat(annotObject.twoD.bndbox.ylmin)
    let xlmax = parseFloat(annotObject.twoD.bndbox.xlmax)
    let ylmax = parseFloat(annotObject.twoD.bndbox.ylmax)
    let rangePath = new Paper.Path()
    rangePath.add(new Paper.Point(xlmin, ylmin))
    rangePath.add(new Paper.Point(xlmax, ylmax))
    // rangePath.strokeColor = elementColor;
    rangePath.strokeColor = 'rgb(255,0,0)'
    rangePath.strokeWidth = 2
    rangePath.strokeScaling = false
    rangePath.closed = false
    rangePath.opacity = 1
    rangePath.markType = 'rect_line'
    rangePath.tag = path.tag
    rangePath.parentLabel = path
    rangePath.customCenter = {
      x: (xlmin + xlmax) / 2,
      y: (ylmin + ylmax) / 2,
    }

    let rangePoint1 = new Paper.Path()
    rangePoint1.add(new Paper.Point(xlmin, ylmin))
    rangePoint1.strokeColor = 'rgb(255,0,0)'
    rangePoint1.strokeWidth = this.pointStrokeWidth
    rangePoint1.strokeScaling = false
    rangePoint1.closed = true
    rangePoint1.opacity = 1
    rangePoint1.markType = 'point'
    rangePoint1.strokeCap = 'round'
    rangePoint1.tag = path.tag
    rangePoint1.customCenter = {
      x: xlmin,
      y: ylmin,
    }
    rangePoint1.pointIndex = 3
    rangePoint1.parentLabel = rangePath
    rangePath.rangePoint1 = rangePoint1

    let rangePoint2 = new Paper.Path()
    rangePoint2.add(new Paper.Point(xlmax, ylmax))
    rangePoint2.strokeColor = 'rgb(255,0,0)'
    rangePoint2.strokeWidth = this.pointStrokeWidth
    rangePoint2.strokeScaling = false
    rangePoint2.closed = true
    rangePoint2.opacity = 1
    rangePoint2.markType = 'point'
    rangePoint2.strokeCap = 'round'
    rangePoint2.tag = path.tag
    rangePoint2.customCenter = {
      x: xlmax,
      y: ylmax,
    }
    rangePoint2.pointIndex = 4
    rangePoint2.parentLabel = rangePath
    rangePath.rangePoint2 = rangePoint2

    path.rangePath = rangePath
  }

  path.mMarkAnnotationId = annotObject.mMarkAnnotationId
  path.pointToLocal = (point) => {
    return this.rasterLayer[imageIndex].globalToLocal(point)
  }
  path.hide = () => {
    // path.point1.opacity = 0;
    // path.point3.opacity = 0;
    path.textObject.opacity = 0
    path.data.baseRect.opacity = 0
    if (path.midPath) {
      path.midPath.opacity = 0
    }
    if (path.rangePath) {
      path.rangePath.opacity = 0
    }
  }
  path.show = () => {
    // path.point1 = 1;
    // path.point3 = 1;
    path.textObject.opacity = 1
    path.data.baseRect.opacity = 1
    if (path.midPath) {
      path.midPath.opacity = 1
    }
    if (path.rangePath) {
      path.rangePath.opacity = 1
    }
  }
  return path
}
